<?php
declare (strict_types = 1);

namespace app\common\controller\cron;

use app\admin\controller\AdminController;
use app\common\model\CronModel;
use app\common\service\CronService;
use app\Request;
use think\facade\View;

/**
 * 计划任务管理
 *
 * @Author tanruizheng
 * @DateTime 2021-07-19
 */
class Cron extends AdminController
{

    /**
     * 计划任务列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function index(Request $request){
        if($request->param('_action') == 'getCronList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $where = [];
            $result = CronService::getCronList($where,$page,$limit);
            return json($result);
        }
        return View::fetch('index',[
            
        ]);
    }

    /**
     * 计划任务添加
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function add(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = CronService::addOrEditData($params);
            return json($result);
        }
        $dayOptions = CronService::getDaysOptions();
        $hoursOptions = CronService::getHoursOptions();
        return View::fetch('add',[
            'dayOptions' => $dayOptions,
            'hoursOptions' => $hoursOptions
        ]);
    }

    /**
     * 计划任务编辑
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function edit(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = CronService::addOrEditData($params);
            return json($result);
        }
        $cronId = $request->param('id',0);
        $detail = CronService::getCronDetail($cronId);
        $dayOptions = CronService::getDaysOptions();
        $hoursOptions = CronService::getHoursOptions();
        return View::fetch('edit',[
            'detail' => $detail,
            'dayOptions' => $dayOptions,
            'hoursOptions' => $hoursOptions
        ]);
    }

    /**
     * 计划任务删除
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function delete(Request $request){
        $cronId = $request->param('id');
        if(!$cronId){
            return self::createFail('参数错误');
        }
        $result = CronService::cronDelete($cronId);
        return json($result);
    }


    /**
     * 执行计划任务
     *
     * @Author tanruizheng
     * @DateTime 2021-07-22
     * @param Request $request
     * @return void
     */
    public function runAction(Request $request){
        set_time_limit(0);
        ignore_user_abort(true);
        $cronId = $request->param('id');
        $cronModel = CronModel::where('cron_id',$cronId)->findOrEmpty();
        if(!$cronModel){
            return self::createFail('计划任务不存在');
        }
        $loopDaytime = $cronModel->loop_daytime;
        list($day,$hour,$minute) = explode('-',$loopDaytime);
        $day = intval($day);
        $hour = intval($hour);
        $minute = intval($minute);
        $nextTime = CronService::getNextTime($cronModel->loop_type,$day,$hour,$minute);
        CronModel::where('cron_id',$cronModel->cron_id)->save([
            'modified_time' => time(),
            'next_time' => $nextTime
        ]);
        $res = CronService::runAction($cronModel);
        if($res){
            return self::createSuccess('计划任务执行成功');
        }else{
            return self::createFail('计划任务执行失败!');
        }
    }
}
